package yukii.com;

import java.io.*;
import java.net.Socket;

/**
 * 处理器
 * Created by yuven on 2017/9/6.
 */

public class processor implements Runnable{
    private Socket socket;
    private InputStream in;
    private PrintStream out;
    private static final  String WEB_ROOT=".";
    processor( Socket socket) throws IOException {
        this.in=socket.getInputStream();
        this.out=new PrintStream(socket.getOutputStream());
    }
    @Override
    public void run() {
        BufferedReader br= new BufferedReader(new InputStreamReader(in));
        try {
            String[] info=br.readLine().split(" ");
            System.out.println("method:"+info[0]+"requestPath:"+info[1]+"HTTP version:"+info[2]);
            sendFile(info[1]);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void sendFile(String path) throws IOException {
        File file= new File(WEB_ROOT+path);
        InputStream in=new FileInputStream(file);
        byte[] content=new byte[(int)file.length()];
        in.read(content);
        out.println("HTTP/1.0 200 queryfile");
        out.println("content-length:"+content.length);
        out.println();
        out.write(content);
        out.flush();
        out.close();
        in.close();
    }
}
